/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkwserver;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;
import org.apache.mina.core.filterchain.DefaultIoFilterChainBuilder;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.codec.textline.TextLineCodecFactory;
import org.apache.mina.filter.ssl.SslFilter;
import org.apache.mina.transport.socket.SocketAcceptor;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

/**
 *
 * @author Pawel
 */
/**
 *
 * @author <a href="http://mina.apache.org">Apache MINA Project</a>
 */
class TrustManagerFactory extends TrustManagerFactorySpi {

    static final X509TrustManager X509 = new X509TrustManager() {

        public void checkClientTrusted(X509Certificate[] x509Certificates,
                String s) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] x509Certificates,
                String s) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }
    };
    static final TrustManager[] X509_MANAGERS = new TrustManager[]{X509};

    public TrustManagerFactory() {
    }

    @Override
    protected TrustManager[] engineGetTrustManagers() {
        return X509_MANAGERS;
    }

    @Override
    protected void engineInit(KeyStore keystore) throws KeyStoreException {
        // noop
    }

    @Override
    protected void engineInit(ManagerFactoryParameters managerFactoryParameters)
            throws InvalidAlgorithmParameterException {
        // noop
    }
}

public class PKWMina {

    private static final int port = 9999;
    private static final String host = "localhost";

    public static void main(String args[]) throws Exception {
        SocketAcceptor acceptor = new NioSocketAcceptor();
        acceptor.setReuseAddress(true);
        //acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();

        // SSL stuff
      //  File pKeyFile = new File("pkwkeystore.jks");
        File pKeyFile = new File("certs" + File.separatorChar + "mycakeystore.jks");
        String pKeyPassword = "bskbsk";
        KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
        KeyStore keyStore = KeyStore.getInstance("JKS");

        InputStream keyInput = new FileInputStream(pKeyFile);
        keyStore.load(keyInput, pKeyPassword.toCharArray());
        keyInput.close();
        keyManagerFactory.init(keyStore, pKeyPassword.toCharArray());

        SSLContext context = SSLContext.getInstance("TLS");

        context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());
        //context.init(keyManagerFactory.getKeyManagers(), TrustManagerFactory.X509_MANAGERS, null);
        // end SSL stuff
        
        SslFilter sslFilter = new SslFilter(context);
        sslFilter.setNeedClientAuth(false);
        sslFilter.setUseClientMode(false);
        chain.addLast("sslFilter", sslFilter);
      
        acceptor.setHandler(new PKWProtocolHandler());
        acceptor.getSessionConfig().setReadBufferSize(8192);
        acceptor.bind(new InetSocketAddress(port));
    }
}